package com.hjm.project.terminal.changeFeeRecord.service.impl;

import java.util.List;

import com.hjm.common.utils.Utils;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.formalPos.branchPos.domain.BranchPos;
import com.hjm.project.formalPos.branchPos.mapper.BranchPosMapper;
import com.hjm.project.formalPos.formalPos.domain.FormalPos;
import com.hjm.project.formalPos.formalPos.mapper.FormalPosMapper;
import org.springframework.stereotype.Service;
import com.hjm.project.terminal.changeFeeRecord.mapper.ChangeFeeRecordMapper;
import com.hjm.project.terminal.changeFeeRecord.domain.ChangeFeeRecord;
import com.hjm.project.terminal.changeFeeRecord.service.IChangeFeeRecordService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 变更通讯费记录Service业务层处理
 * 
 * @author maker-platform
 * @date 2023-04-03
 */
@Service
public class ChangeFeeRecordServiceImpl implements IChangeFeeRecordService {
    @Resource
    private ChangeFeeRecordMapper changeFeeRecordMapper;
    @Resource
    private FormalPosMapper formalPosMapper;
    @Resource
    private BranchPosMapper branchPosMapper;

    /**
     * 查询变更通讯费记录
     * 
     * @param sysId 变更通讯费记录ID
     * @return 变更通讯费记录
     */
    @Override
    public ChangeFeeRecord selectChangeFeeRecordById(Integer sysId) {
        return changeFeeRecordMapper.selectChangeFeeRecordById(sysId);
    }

    /**
     * 查询变更通讯费记录列表
     * 
     * @param changeFeeRecord 变更通讯费记录
     * @return 变更通讯费记录
     */
    @Override
    public List<ChangeFeeRecord> selectChangeFeeRecordList(ChangeFeeRecord changeFeeRecord) {
        return changeFeeRecordMapper.selectChangeFeeRecordList(changeFeeRecord);
    }

    /**
     * 查询变更通讯费记录列表
     *
     * @param changeFeeRecord 变更通讯费记录
     * @return 变更通讯费记录 一个结果
     */
    @Override
    public ChangeFeeRecord selectChangeFeeRecordOne(ChangeFeeRecord changeFeeRecord) {
        return changeFeeRecordMapper.selectChangeFeeRecordOne(changeFeeRecord);
    }

    /**
     * 新增变更通讯费记录
     * 
     * @param changeFeeRecord 变更通讯费记录
     * @return 结果
     */
    @Override
    public int insertChangeFeeRecord(ChangeFeeRecord changeFeeRecord) {
        try {
            //获取所有终端
            List<String> poss = changeFeeRecordMapper.getPosByAgent(changeFeeRecord);
            int i = 0;
            for(String pos :poss){
                //获取终端详细信息
                FormalPos formalPos = formalPosMapper.selectFormalPosByPosNo(pos);
                BranchPos branchPos = branchPosMapper.selectBranchPosByPosno(pos);
                if(formalPos != null){
                    //拼装参数
                    changeFeeRecord.setMachineNum(branchPos.getMachineNum());           //sn编号
                    changeFeeRecord.setPosno(branchPos.getPosno());                     //posno
                    changeFeeRecord.setOldSimFee(formalPos.getSimFee().toString());     //原流量卡费
                    changeFeeRecord.setOldReturnAgentNum(branchPos.getFlowAgentNum());  //原返现代理商
                    changeFeeRecord.setActivateStatus(formalPos.getActivate());         //激活状态
                    changeFeeRecord.setReturnAgentNum("100000000");                     //返现代理商编号
                    changeFeeRecord.setMerno(formalPos.getMerno());                     //商户编号
                    changeFeeRecord.setAddDate(Utils.formateDate(2));             //录入时间
                    if(changeFeeRecordMapper.getBankReturnCount(changeFeeRecord) <= 0 && changeFeeRecordMapper.getChangeFeeCount(changeFeeRecord) <= 0){
                        //变更终端流量卡费
                        i = changeFeeRecordMapper.updateSnSimFeeBp(changeFeeRecord);
                        i= changeFeeRecordMapper.updateSnSimFeeFp(changeFeeRecord);
                        //保存录入信息
                        i= changeFeeRecordMapper.insertChangeFeeRecord(changeFeeRecord);
                    }
                }
            }
            return i;
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改变更通讯费记录
     * 
     * @param changeFeeRecord 变更通讯费记录
     * @return 结果
     */
    @Override
    public int updateChangeFeeRecord(ChangeFeeRecord changeFeeRecord) {
        return changeFeeRecordMapper.updateChangeFeeRecord(changeFeeRecord);
    }

    /**
     * 删除变更通讯费记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteChangeFeeRecordByIds(String ids) {
        return changeFeeRecordMapper.deleteChangeFeeRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除变更通讯费记录信息
     * 
     * @param changeFeeRecord 变更通讯费记录ID
     * @return 结果
     */
    @Override
    public int deleteChangeFeeRecordById(ChangeFeeRecord changeFeeRecord) {
        int count = changeFeeRecordMapper.getBankReturnCount(changeFeeRecord);
        ChangeFeeRecord c = changeFeeRecordMapper.selectChangeFeeRecordOne(changeFeeRecord);
        if(count > 0){
            return -2;
        }
        c.setNowSimFee(c.getOldSimFee());
        c.setReturnAgentNum(c.getOldReturnAgentNum());
        //变更终端流量卡费
        changeFeeRecordMapper.updateSnSimFeeBp(c);
        changeFeeRecordMapper.updateSnSimFeeFp(c);
        count = changeFeeRecordMapper.deleteChangeFeeRecordById(changeFeeRecord);
        return count;

    }

    /**
     *
     * @param agentInfo
     * @return
     */
    @Override
    public List<ChangeFeeRecord> getAgentInfoList(AgentInfo agentInfo){
        return changeFeeRecordMapper.getAgentInfoList(agentInfo);
    }

    public int getSnCount(ChangeFeeRecord changeFeeRecord){
         List list = changeFeeRecordMapper.getPosByAgent(changeFeeRecord);
        return list.size();
    }
}
